package com.atguigu.lease.web.admin.schedule;

import ch.qos.logback.core.net.server.Client;
import com.atguigu.lease.common.minio.MinioProperties;
import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.GraphInfoService;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mysql.cj.util.StringUtils;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Component
public class ScheduledTasks {

    @Autowired
    private LeaseAgreementService leaseAgreementService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    MinioClient minioClient;
    @Autowired
    MinioProperties minioProperties;

    @Autowired
    RedisTemplate redisTemplate;

    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus() {
        Date now = new Date();
        LambdaUpdateWrapper<LeaseAgreement> leaseAgreementLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        leaseAgreementLambdaUpdateWrapper.lt(LeaseAgreement::getLeaseEndDate, now);
        leaseAgreementLambdaUpdateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
        leaseAgreementLambdaUpdateWrapper.set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
        leaseAgreementService.update(leaseAgreementLambdaUpdateWrapper);
    }

//    @Scheduled(cron = "0 0/5 * * * *")
    public void checkRemoveMinioImage() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        List<String> urlList =  graphInfoService.removeByIsDeleted();

        for (String url : urlList) {
            if (!StringUtils.isNullOrEmpty(url)){
                String fileName = url.substring(url.lastIndexOf("lease/")+6);
                minioClient.removeObject(RemoveObjectArgs.builder().bucket(minioProperties.getBucketName()).
                        object(fileName).build());
            }

        }
        String allImages = "allImages:key";
        String dbImageKey = "db_image:key";
        Set difference = redisTemplate.opsForSet().difference(allImages, dbImageKey);
        for (Object fileName : difference) {
            minioClient.removeObject(RemoveObjectArgs.builder().bucket(minioProperties.getBucketName()).
                    object(fileName.toString()).build());
        }
        System.out.println("执行了定时任务");
    }
}